package cn.yujiago.activiti.util;

import cn.yujiago.activiti.constant.UserConstant;
import cn.yujiago.activiti.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * session工具类
 */
public class SessionUtils {
	
	private static final String GLOBLE_SESSION_KEY = UserConstant.LOGIN_USER_KEY;
	
	public static User get() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return (User) request.getSession().getAttribute(GLOBLE_SESSION_KEY);
	}

	public static void set(User user) {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		if(user != null) {
			request.getSession().setAttribute(GLOBLE_SESSION_KEY, user);
		}else{
			request.getSession().removeAttribute(GLOBLE_SESSION_KEY);
		}
	}
}
